package com.ChengXiZi.controller.user;

import com.ChengXiZi.constant.JwtClaimsConstant;
import com.ChengXiZi.dto.UserAddDTO;
import com.ChengXiZi.dto.UserLoginDto;
import com.ChengXiZi.dto.UserPageDTO;
import com.ChengXiZi.entity.User;
import com.ChengXiZi.properties.JwtProperties;
import com.ChengXiZi.result.PageResult;
import com.ChengXiZi.result.Result;
import com.ChengXiZi.service.UserService;
import com.ChengXiZi.utils.JwtUtil;
import com.ChengXiZi.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 普通用户接口
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户接口")
public class UserContruller {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 普通用户登录
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login (@RequestBody UserLoginDto userLoginDto){
        log.info("普通用户登录：{}",userLoginDto);

        User user = userService.login(userLoginDto);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .name(user.getName())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 创建用户
     * @param userAddDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("创建用户")
    public Result save(@RequestBody UserAddDTO userAddDTO){
        log.info("创建用户: {}",userAddDTO);
        userService.save(userAddDTO);
        return Result.success();
    }

    /**
     * 查询用户
     * @param userPageDTO
     * @return
     */
    @GetMapping("/search/user")
    @ApiOperation("查询用户")
    public Result<PageResult> page(UserPageDTO userPageDTO){
        log.info("分页查询，参数：{}",userPageDTO);
        PageResult pageResult = userService.pageQuery(userPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 社区管理员封禁普通用户账号
     * @param status
     * @param targetid
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("社区管理员封禁普通用户账号")
    public Result startOrStop(@PathVariable Integer status, Long targetid, Long userId){
        log.info("社区管理员封禁普通用户账号：status：{},id：{}",status,targetid);
        userService.startOrStop(status,targetid,userId);
        return Result.success();
    }
}
